/*
 * @Author: gitee_com_zb
 * @Date: 2024-12-12 17:08:35
 * @LastEditors: gitee_com_zb
 * @LastEditTime: 2024-12-12 17:08:47
 * @FilePath: /algorithm/每日一题12.LCR字符串的排列(medium).cpp
 * @Description: 题目链接 https://leetcode.cn/problems/MPnaiL?envType=problem-list-v2&envId=sliding-window
 */
class Solution {
public:
    bool checkInclusion(string s1, string s2) {
        if (s1.size() > s2.size()) return false;
        unordered_map<char, int> hash1, hash2;
        for(auto &x : s1) hash1[x]++;
        for(int i = 0; i < s2.size(); i++) {
            hash2[s2[i]]++;
            if(i >= s1.size()) {
                char left = s2[i - s1.size()];
                hash2[left]--;
                if(hash2[left] == 0) hash2.erase(left);
            }
            if(hash1 == hash2) return true;
        }
        return false;
    }
};